package com.appspot.simplebookmarks.controller.bookmark;

import java.util.List;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import com.appspot.simplebookmarks.model.Bookmark;
import com.appspot.simplebookmarks.model.Collection;
import com.appspot.simplebookmarks.service.BookmarkService;
import com.appspot.simplebookmarks.service.CollectionService;
import com.appspot.simplebookmarks.service.LastAccessCollectionService;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class IndexController extends Controller {

    private BookmarkService bookmarkService = new BookmarkService();
    private CollectionService collectionService = new CollectionService();
    private LastAccessCollectionService lastAccessCollectionService = new LastAccessCollectionService();

    @Override
    public Navigation run() throws Exception {
        
        //ユーザー情報取得とログインチェック
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        if (user == null) {
            return redirect(userService.createLoginURL("/bookmark/"));
        }

        //コレクション一覧の取得
        List<Collection> collections = collectionService.getList(user.getEmail());

        //カレントコレクションの取得
        Collection currentCollection = this.getCurrentCollection(user.getEmail(), collections);

        //ブックマーク一覧の取得
        List<Bookmark> bookmarks = bookmarkService.getList(user.getEmail(), currentCollection.getKey());

        //jspへ値を渡す
        requestScope("author", user.getEmail());
        requestScope("logout", userService.createLogoutURL("/"));
        requestScope("collections", collections);
        requestScope("currentCollection", currentCollection);
        requestScope("bookmarks", bookmarks);

        return forward("index.jsp");
    }

    /*
     * カレントコレクションの取得
     */
    private Collection getCurrentCollection(String author, List<Collection> collections) {
        Collection currentCollection = collections.get(0);
        String c = asString("c");
        if (c != null) {
            currentCollection = 
                this.getCollectionFromCollections(collections, KeyFactory.stringToKey(c));
            lastAccessCollectionService.updateLastAccess(author, currentCollection.getKey());
        } else {
            Key key = lastAccessCollectionService.getCurrentCollectionKey(author);
            currentCollection = this.getCollectionFromCollections(collections, key);
        }
        return currentCollection;
    }

    /*
     * 指定したキーにマッチするコレクションを取得
     */
    private Collection getCollectionFromCollections(List<Collection> collections, Key key) {
        for (Collection e:collections) {
            if (e.getKey().equals(key)) {
                return e;
            }
        }
        return null;
    }
}
